package com.info.zhiduoduo.admin.model.formbean.company;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.mapper.Condition;
import com.info.zhiduoduo.repository.dto.company.CompanyTaxDto;
import com.info.zhiduoduo.repository.dto.quota.CompanyQuotaDto;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.company.CompanyAccount;
import com.info.zhiduoduo.repository.entity.company.CompanyParty;
import com.info.zhiduoduo.repository.entity.partner.PartnerAccount;
import com.info.zhiduoduo.repository.entity.system.BusinessManager;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 企业设置
 * @author tianyonghong
 */
@Data
public class CompanySetting {

    @ApiModelProperty("企业ID")
    private Integer compid;

    @ApiModelProperty("设置模块编码：基础信息:base; 业务设置:zdd;智众包业务设置:cro;智零工业务设置:odd 创客团业务：group")
    @NotNull(message = "模块编码不能为空")
    private String module;

    @ApiModelProperty("基础信息")
    private BaseModule baseModule;

    //企业限额设置 com.info.zhiduoduo.admin.service.QuotaService.editCompanyQuota
    @ApiModelProperty("企业限额设置(查询时返回)")
    private CompanyQuotaDto companyQuotaDto;

    @ApiModelProperty("创客团设置")
    private BusinessGroupModule businessGroupModule;

    @ApiModelProperty(" 业务设置")
    private BusinessZddModule businessZddModule;

    // 企业费率设置 com.info.zhiduoduo.admin.controller.CompanyTaxController.editCompanyTax
    @ApiModelProperty(" 企业费率设置(查询时返回)")
    private CompanyTaxDto companyTaxDto;

    @ApiModelProperty("智众包业务设置")
    private BusinessCroModule businessCroModule;

    @ApiModelProperty("智零工业务设置")
    private BusinessOddModule businessOddModule;

    //企业合伙人设置 com.info.zhiduoduo.admin.controller.PartnerController.savePartnerCompany

    @Data
    @Accessors(chain = true)
    public class BaseModule {
        @ApiModelProperty("企业账户id")
        private Integer acctid;

        @ApiModelProperty("是否可交易(0待设置，1已初步设置，2设置完成)")
        private Integer tradeFlag;

        @ApiModelProperty("交易审核人员手机号")
        private String verifyPhone;

        @ApiModelProperty("是否开通PC企业：0：关，1.开")
        private Integer whetherOpen;

        @ApiModelProperty("是否显示招商银行记账子单元 0：否 1：是")
        private Integer showZhaoshangLedger;

        @ApiModelProperty("是否为主体公司：0.否，1.是")
        private Integer whetherSubject;

        @ApiModelProperty("是否友好企业标识(0:未标记,1:友好)")
        private Integer friendlyState;

        @ApiModelProperty("是否开通委托接单功能(0:否,1:是)")
        private Integer entrustedReceiptStatus;

        @ApiModelProperty("是否代理操作账户标识(0:否,1:是)")
        private Integer agentOperationAccount;

        @ApiModelProperty("是否自动冻结(0:是,1否)")
        private Integer autoFreeze;

        @ApiModelProperty("有限责任公司 0否 1是")
        private Integer limitedLiabilityCompany;

        @ApiModelProperty("是否接收公告 0：正常 1：不接受")
        private Integer noticeFlag;

        @ApiModelProperty("策略性开票 0:否 1:是")
        private Integer strategyInvoice;

        @ApiModelProperty("特殊费率标记 0:否 1:是")
        private Integer specialRateFlag;

        @ApiModelProperty(" 模式是否开启0：否， 1：是")
        private Boolean openZdd;

        @ApiModelProperty("智众包模式是否开启 1：否 2：开启")
        private Integer intelCrowdsource;

        @ApiModelProperty("是否可查看创客可用额度（0否，1是）")
        private Integer viewMakerQuota;

        @ApiModelProperty("专业顾问ID")
        private Integer businessManagerId;

        @ApiModelProperty("专业顾问名称")
        private String businessManagerName;

        @ApiModelProperty("跟单员ID")
        private Integer merchandiserId;

        @ApiModelProperty("跟单员名称")
        private String merchandiserName;

        @ApiModelProperty("备注")
        private String changeNotes;

        @ApiModelProperty("无感收款(0否，1是)")
        private Integer noFeelIncome;

        @ApiModelProperty("账号关联标签ID")
        private Integer accountLinkId;

        @ApiModelProperty("账号关联普通标签ID")
        private String normalLinkIds;
    }

    @Data
    @Accessors(chain = true)
    public class BusinessZddModule {
        @ApiModelProperty("是否开通 0：否， 1：是")
        private Boolean openZdd;

        @ApiModelProperty(" 业务支付方式（0:线上 1线下）")
        private Integer payMethodZdd;

        @ApiModelProperty(" 业务主体设置")
        private List<CompanyParty> companyPartyList;
    }

    @Data
    @Accessors(chain = true)
    public class BusinessCroModule {
        @ApiModelProperty("智众包模式是否开启 1：否 2：开启")
        private Integer intelCrowdsource;

        @ApiModelProperty("智众包业务支付方式（0:线上 1线下）")
        private Integer payMethodCro;

        @ApiModelProperty("智众包业务支付方式/主体设置")
        private List<CompanyParty> companyPartyList;

        //众包基础配置
        @ApiModelProperty("智众包阶梯式费率 1:否 2:开启")
        private Integer crowdsourceStepped;

        @ApiModelProperty("合作模式 1: 代付模式 2：承包模式")
        private Integer crowdsourceType;

        @ApiModelProperty("基础税率")
        private Double baseTaxRate;

        @ApiModelProperty("年度25万以下税率")
        private Double lowTaxRate;

        @ApiModelProperty("年度25万-75万税率")
        private Double highTaxRate;

        @ApiModelProperty("是否开启保证金 1:否 2:是")
        private Integer depositFlag;

        @ApiModelProperty("保证金比例")
        private Double depositRate;

        @ApiModelProperty("保证金返回周期(1-6月)")
        private Integer returnPeriod;

        @ApiModelProperty("发票类型 1:普票 2:专票")
        private Integer invoiceType;

        @ApiModelProperty("开票内容")
        private String invoiceScope;

        @ApiModelProperty("众包成本合伙人id")
        private Integer crowdsourcePartnerId;

        @ApiModelProperty("众包成本合伙人名称")
        private String crowdsourcepartnerName;
    }


    @Data
    @Accessors(chain = true)
    public class BusinessOddModule {
        @ApiModelProperty("是否开通智零工(0:关（默认），1:开启)")
        private Integer openOddJob;

        @ApiModelProperty("智零工基础税率")
        private Double baseTaxRateOdd;

        @ApiModelProperty("智零工业务支付方式（0:线上 1线下）")
        private Integer payMethodOdd;

        @ApiModelProperty("智零工开票内容")
        private String invoiceScopeOdd;

        @ApiModelProperty("智零工业务支付方式/主体设置")
        private List<CompanyParty> companyPartyList;
    }

    @Data
    @Accessors(chain = true)
    public class BusinessGroupModule {
        @ApiModelProperty("是否开启创客团功能 0:否 1:是")
        private Integer makerGroupon;

        @ApiModelProperty("创客团业务支付方式/主体设置")
        private CompanyParty companyParty;
    }

    // 企业信息转换
    public CompanySetting format(CompanyAccount account, Company company) {
        CompanySetting setting = new CompanySetting();
        setting.setCompid(company.getCompid());

        // 基础
        BaseModule baseModule = new BaseModule();
        BeanUtils.copyProperties(company, baseModule);
        BeanUtils.copyProperties(account, baseModule);
        // 商务经理名称
        Integer managerId = account.getBusinessManagerId();
        Integer merchandiserId = account.getMerchandiserId();
        if (managerId != null) {
            BusinessManager businessManager = new BusinessManager().selectById(managerId);
            if (businessManager != null) {
                baseModule.setBusinessManagerName(businessManager.getBusinessManager());
            }
        }
        if (merchandiserId != null) {
            BusinessManager businessManager = new BusinessManager().selectById(managerId);
            if (businessManager != null) {
                baseModule.setMerchandiserName(businessManager.getBusinessManager());
            }
        }


        //  业务
        BusinessZddModule businessZddModule = new BusinessZddModule().setOpenZdd(company.getOpenZdd()).setPayMethodZdd(account.getPayMethodZdd());
        // 智众包业务
        BusinessCroModule businessCroModule = new BusinessCroModule();
        BeanUtils.copyProperties(company, businessCroModule);
        BeanUtils.copyProperties(account, businessCroModule);
        // 合伙人名称：
        if (company.getCrowdsourcePartnerId() != 0) {
            PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", company.getCrowdsourcePartnerId()));
            if(partnerAccount != null){
                businessCroModule.setCrowdsourcepartnerName(partnerAccount.getRealname());
            }
        }

        // 智零工业务
        BusinessOddModule businessOddModule = new BusinessOddModule();
        BeanUtils.copyProperties(company, businessOddModule);

        // 创客团
        BusinessGroupModule businessGroupModule = new BusinessGroupModule().setMakerGroupon(account.getMakerGroupon());

        setting.setBaseModule(baseModule);
        setting.setBusinessZddModule(businessZddModule);
        setting.setBusinessCroModule(businessCroModule);
        setting.setBusinessOddModule(businessOddModule);
        setting.setBusinessGroupModule(businessGroupModule);
        return setting;
    }
}
